package com.thti.mecool.client.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.thti.mecool.client.activity.R;
import com.thti.mecool.client.activity.TaskdoneDetailActivity;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.model.biz.TaskBizImpl;
import com.thti.mecool.client.util.StringUtil;

public class MutilListAdapter extends BaseExpandableListAdapter {
	ArrayList<ArrayList<HashMap<String, String>>> taskList;
	ArrayList<String> listTag;
	int listCount;
	boolean IsSync,IsDataSyncEcho;
	int index;
	Context mContext;
	HashMap<Integer, String> contents=new HashMap<Integer, String>();
	ArrayList<ArrayList<Integer>> stat=null;
	public MutilListAdapter(
			ArrayList<ArrayList<HashMap<String, String>>> taskList,
			ArrayList<String> listTag, Context context) {
		this.taskList = taskList;
		this.listTag = listTag;
		mContext = context;
	}

	public MutilListAdapter(
			ArrayList<ArrayList<HashMap<String, String>>> taskList,
			ArrayList<String> listTag,ArrayList<ArrayList<Integer>> stat, Context context) {
		this.taskList = taskList;
		this.listTag = listTag;
		this.stat=stat;
		mContext = context;
	}
	@Override
	public Object getChild(int groupPosition, int childPosition) {

		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		return null;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return listTag.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return listTag.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView==null){
			holder=new ViewHolder();
		convertView = LayoutInflater.from(mContext).inflate(
				R.layout.mecool_tasklist_item, null);
		holder.time = (TextView) convertView
				.findViewById(R.id.mecool_tasklist_itemtime);
		holder.name = (TextView) convertView
				.findViewById(R.id.mecool_tasklist_itemname);
		holder.addr = (TextView) convertView
				.findViewById(R.id.mecool_tasklist_itemaddr);
		holder.tag = (TextView) convertView
				.findViewById(R.id.mecool_tasklist_projectName);
		holder.undone_reason=(EditText)convertView.findViewById(R.id.mecool_undone_reason);
		holder.SyncText=(TextView)convertView.findViewById(R.id.mecool_tasklist_itemsync);
		holder.Syncbtn=(Button)LayoutInflater.from(mContext).inflate(R.layout.mecool_taskmanage, null).findViewById(R.id.mecool_SyncBtn);
		holder.taskCheck=(CheckBox)convertView.findViewById(R.id.mecool_tasklist_itemchecked);
		convertView.setTag(holder);
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		holder.time.setText(taskList.get(groupPosition).get(0).get("time"));
		holder.name.setText(taskList.get(groupPosition).get(0).get("name"));
		holder.addr.setText(taskList.get(groupPosition).get(0).get("addr"));
		holder.tag.setText("("+listTag.get(groupPosition)+")");
		if(taskList.get(groupPosition).get(0).get("isType")!=null){
			if(taskList.get(groupPosition).get(0).get("isType").equals("assignment")){
				holder.tag.setTextColor(Color.RED);
			}
		}else{
			holder.tag.setTextColor(Color.DKGRAY);
		}
		if(mContext.getClass().equals(TaskdoneDetailActivity.class)){
			holder.undone_reason.setVisibility(0);
			holder.undone_reason.setTag(groupPosition);
			holder.undone_reason.setOnTouchListener(onTouchListener);
			holder.undone_reason.addTextChangedListener(textWatcher);
			holder.tag.setVisibility(8);
			holder.name.setText(holder.name.getText()+" "+"("+listTag.get(groupPosition)+")");
		}
		PlanBizImpl planBizImpl=new PlanBizImpl(mContext);
//		List<PlanClient> plans=(List<PlanClient>) planBizImpl.findByType(TimeUtil.dateToYear());
//		StringBuffer sb=new StringBuffer();
//		sb.append("select * from task");
//		for(PlanClient pc:plans){
//			pc.getPlanId();
//		}
		if(stat!=null&&stat.size()>0){
			TaskBizImpl taskBizImpl=new TaskBizImpl(mContext);
			int isUpload=taskBizImpl.findUpload(taskList.get(groupPosition).get(0).get("planId"),taskList.get(groupPosition).get(0).get("taskId"));
//			if(stat.size()>groupPosition){
				if(isUpload==2){
					holder.SyncText.setVisibility(View.VISIBLE);
				}else{
					holder.SyncText.setVisibility(View.GONE);
				}
//			}
		}
		return convertView;
	}
	OnTouchListener onTouchListener=new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_UP) {
				index = (Integer) v.getTag();
				System.out.println("index   :   "+v.getClass());
			}
			return false;
		}
	};
	private TextWatcher textWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
		}
		@Override
		public void afterTextChanged(Editable arg0) {
			if(StringUtil.isBlank(arg0.toString().trim()))
				contents.remove(index);
			else
			contents.put(index, arg0.toString().trim());
		}
	};
	@Override
	public boolean hasStableIds() {
		return false;
	}

	public HashMap<Integer, String> findContents(){
		return contents;
	}
	
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	static class ViewHolder{

		EditText undone_reason;
		Button Syncbtn;
		CheckBox taskCheck;
		TextView tag,addr,name,time,SyncText;
	}
	public void setStat(ArrayList<ArrayList<Integer>> stat) {
		this.stat=stat;
	}
}
